/*
 * @Author: xuhua
 * @Date: 2023-09-13 09:49:26
 * @LastEditors: xuhua
 * @LastEditTime: 2023-09-19 18:09:29
 * @FilePath: /v3-element-admin/vite.config.ts
 * @Description:
 */
import { ConfigEnv, UserConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";

import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

import vueSetupExtend from "vite-plugin-vue-setup-extend";
import { viteMockServe } from "vite-plugin-mock";

import UnoCSS from "unocss/vite";

import path from "path";

const pathSrc = path.resolve(__dirname, "src");

// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfig => {
	const env = loadEnv(mode, process.cwd());

	return {
		resolve: {
			alias: {
				"@": pathSrc,
				"@views": path.resolve(__dirname, "src/views"),
				"@api": path.resolve(__dirname, "src/api"),
			},
		},
		server: {
			host: "0.0.0.0",
			port: Number(env.VITE_APP_PORT),
			open: true,
			proxy: {
				[env.VITE_APP_BASE_API]: {
					target: `${env.VITE_APP_TARGET_URL}`,
					changeOrigin: true,
					rewrite: (path) => path.replace(new RegExp(`^/${env.VITE_APP_BASE_API}`), ""),
				},
			},
		},
		plugins: [
			vue(),
			UnoCSS({}),
			vueSetupExtend(),
			AutoImport({
				imports: ["vue", "vue-router", "pinia", "@vueuse/core"],
				resolvers: [
					ElementPlusResolver(), // 自动导入 Element Plus 相关函数，如：ElMessage, ElMessageBox... (带样式)
					IconsResolver({
						prefix: "icon",
					}), // 自动导入图标组件
				],
				eslintrc: {
					enabled: true,
					filepath: "./.eslintrc-auto-import.json",
					globalsPropValue: true,
				},
				vueTemplate: true, // 是否在 vue 模板中自动导入
				// dts: false, // 配置文件生成位置(false:关闭自动生成)
				dts: "src/types/auto-imports.d.ts",
			}),
			Components({
				resolvers: [
					ElementPlusResolver(), // 自动导入 Element Plus 组件
					IconsResolver({
						prefix: "icon",
						enabledCollections: ["ep"], // element-plus图标库，其他图标库 https://icon-sets.iconify.design/
					}),
				],
				dirs: ["src/components"], // 指定自定义组件位置(默认:src/components)
				// dts: false, // 配置文件位置(false:关闭自动生成)
				dts: "src/types/components.d.ts",
			}),
			Icons({
				// 自动安装图标库
				autoInstall: true,
			}),
			createSvgIconsPlugin({
				// 指定需要缓存的图标文件夹
				iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
				// 指定symbolId格式
				symbolId: "icon-[dir]-[name]",
			}),
			viteMockServe({
				mockPath: "./mock/", // 设置模拟数据的存储文件夹
				logger: true, //  是否在控制台显示请求日志
				watchFiles: true, //监听mock文件夹内文件修改
			}),
		],
		css: {
			preprocessorOptions: {
				scss: {
					additionalData: `@use "@/styles/variables.scss" as *;`,
				},
			},
		},
		// 预加载项目必需的组件
		optimizeDeps: {
			include: [
				"vue",
				"vue-router",
				"pinia",
				"axios",
				"element-plus/es/components/form/style/css",
				"element-plus/es/components/form-item/style/css",
				"element-plus/es/components/button/style/css",
				"element-plus/es/components/input/style/css",
				"element-plus/es/components/input-number/style/css",
				"element-plus/es/components/switch/style/css",
				"element-plus/es/components/upload/style/css",
				"element-plus/es/components/menu/style/css",
				"element-plus/es/components/col/style/css",
				"element-plus/es/components/icon/style/css",
				"element-plus/es/components/row/style/css",
				"element-plus/es/components/tag/style/css",
				"element-plus/es/components/dialog/style/css",
				"element-plus/es/components/loading/style/css",
				"element-plus/es/components/radio/style/css",
				"element-plus/es/components/radio-group/style/css",
				"element-plus/es/components/popover/style/css",
				"element-plus/es/components/scrollbar/style/css",
				"element-plus/es/components/tooltip/style/css",
				"element-plus/es/components/dropdown/style/css",
				"element-plus/es/components/dropdown-menu/style/css",
				"element-plus/es/components/dropdown-item/style/css",
				"element-plus/es/components/sub-menu/style/css",
				"element-plus/es/components/menu-item/style/css",
				"element-plus/es/components/divider/style/css",
				"element-plus/es/components/card/style/css",
				"element-plus/es/components/link/style/css",
				"element-plus/es/components/breadcrumb/style/css",
				"element-plus/es/components/breadcrumb-item/style/css",
				"element-plus/es/components/table/style/css",
				"element-plus/es/components/tree-select/style/css",
				"element-plus/es/components/table-column/style/css",
				"element-plus/es/components/select/style/css",
				"element-plus/es/components/option/style/css",
				"element-plus/es/components/pagination/style/css",
				"element-plus/es/components/tree/style/css",
				"element-plus/es/components/alert/style/css",
				"element-plus/es/components/radio-button/style/css",
				"element-plus/es/components/checkbox-group/style/css",
				"element-plus/es/components/checkbox/style/css",
				"element-plus/es/components/tabs/style/css",
				"element-plus/es/components/tab-pane/style/css",
				"element-plus/es/components/rate/style/css",
				"element-plus/es/components/date-picker/style/css",
				"element-plus/es/components/notification/style/css",
				"@vueuse/core",
				"sortablejs",
				"path-to-regexp",
				"echarts",
				"@wangeditor/editor",
				"@wangeditor/editor-for-vue",
				"vue-i18n",
			],
		},
		// 构建
		build: {
			chunkSizeWarningLimit: 2000, // 消除打包大小超过500kb警告
			minify: "terser", // Vite 2.6.x 以上需要配置 minify: "terser", terserOptions 才能生效
			terserOptions: {
				compress: {
					keep_infinity: true, // 防止 Infinity 被压缩成 1/0，这可能会导致 Chrome 上的性能问题
					drop_console: true, // 生产环境去除 console
					drop_debugger: true, // 生产环境去除 debugger
				},
				format: {
					comments: false, // 删除注释
				},
			},
		},
	};
};
